package com.zrx.app.android.UTIL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

/**
 * Created by liuji on 2017/5/16.
 */

public class ImageUTIL {
    public static Bitmap fitToImageView(ImageView imgView,String mCurrentPhotoPath){
        int targetW = imgView.getWidth();
        int targetH = imgView.getHeight();
        return scaleBitmap(mCurrentPhotoPath,targetW,targetH);
    }

    public static Bitmap scaleBitmap(String mCurrentPhotoPath,int targetW,int targetH) {
        // Get the dimensions of the View


        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        return bitmap;
    }
}
